import fs from 'fs'
import { fileURLToPath } from 'url'


//用于获取指定目录下所有以 “Controllers.js” 结尾的文件路径
export const getControllers = function(){
    let controllersUrl= new URL('../controllers',import.meta.url).href
    let filePath=fileURLToPath(controllersUrl)
    let files=fs.readdirSync(filePath)
    let controllersFiles=files.filter(item=>item.endsWith('Controllers.js'))
    let controllersPath=controllersFiles.map(item=>`${controllersUrl}/${item}`)

    return controllersPath

}

//批量注册路由
export const regControllers=function(router,controllersFiles){
        controllersFiles.forEach(async item => {
        let arr=await import(item)
            arr.default.forEach(obj=>{
                if(obj.method==='get'){
                    router.get(obj.url,obj.fn)
                }
                if(obj.method==='post'){
                    router.post(obj.url,obj.fn)
                }
                if(obj.method==='put'){
                    router.put(obj.url,obj.fn)
                }
                if(obj.method==='del'){
                    router.delete(obj.url,obj.fn)
                }

            })

        })
   
       
}   